package jp.bsws.action.master;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpSession;

import jp.bsws.bean.GenkinShutsunoBean;
import jp.bsws.bean.JugyoinBean;
import jp.bsws.bean.KeyValueBean;
import jp.bsws.common.BSWSConstants;
import jp.bsws.common.BSWSSession;
import jp.bsws.common.BSWSUtils;
import jp.bsws.common.DBUtils;
import jp.bsws.dao.BSWSDao;
import jp.bsws.dao.GenkinShutsunoDao;
import jp.bsws.dao.KengenDao;
import jp.bsws.model.GenkinShutsunoModel;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

/*
 * メインメニューアクションクラス
 */
public class GenkinShutsunoMasterAction extends ActionSupport {
	private static final long serialVersionUID = 1L;

	private BSWSSession bSWSSession;

	public String getLoginJugyoinKnj() { return bSWSSession.getStrJugyoinKnj(); }

	private List<GenkinShutsunoBean> genkinShutsunoBeanList;
	private Integer intTaisyoSeq;

	public void validateUpdateRaitenYoin() {

		//セッション情報を取得
		HttpSession session = ServletActionContext.getRequest().getSession();
		bSWSSession = (BSWSSession) session.getAttribute(BSWSConstants.KEY_SESSION);

		//必須チェック
//		for(int i =0; i < raitenYoinBeanList.size(); i++){
//			RaitenYoinBean raitenYoinBean = raitenYoinBeanList.get(i);
//			if(intTaisyoSeq == raitenYoinBean.getSeq() && raitenYoinBean.isChkEdit()){
//				if (BSWSUtils.isBlankNull(raitenYoinBean.getStrRaitenYoinKnj())) {
//					addFieldError("raitenYoinBeanList[" + i + "].strRaitenYoinKnj", (i + 1) + "行目の来店要因名称を入力して下さい。");
//				}
//				break;
//			}
//		}
	}

	private void init() throws Exception{
		//セッション情報を取得
		HttpSession session = ServletActionContext.getRequest().getSession();
		bSWSSession = (BSWSSession) session.getAttribute(BSWSConstants.KEY_SESSION);
	}

	@Override
	public String execute() throws Exception{

		//初期設定
		init();

		Connection con = DBUtils.getConnection();
		try{
			//画面表示内容の設定
			hyojiJunbi(con);
		}finally{
			con.close();
		}

		return SUCCESS;
	}

	/**
	 * 現金出納追加
	 * @return SUCCESS
	 * @throws Exception
	 */
	public String add() throws Exception{

		init();

		for(GenkinShutsunoBean genkinShutsunoBean : this.genkinShutsunoBeanList) {
			genkinShutsunoBean.setStrGroupCod(bSWSSession.getStrGroupCod());
			genkinShutsunoBean.setHyojiKbnList(BSWSConstants.hyojiKbnList);
		}

		//現金出納行追加
		GenkinShutsunoBean genkinShutsunoBean = new GenkinShutsunoBean();
		genkinShutsunoBean.setStrGroupCod(bSWSSession.getStrGroupCod());
		genkinShutsunoBean.setHyojiKbnList(BSWSConstants.hyojiKbnList);
		this.genkinShutsunoBeanList.add(genkinShutsunoBean);

		return SUCCESS;
	}

	public String updateGenkinShutsuno() throws Exception{

		GenkinShutsunoDao genkinShutsunoDao = GenkinShutsunoDao.getInstance();

		//初期設定
		init();

		Connection con = DBUtils.getConnection();
		try{

			//現金出納情報更新対象取得
			for(GenkinShutsunoBean genkinShutsunoBean : genkinShutsunoBeanList) {
				int cnt = 0;
				if(genkinShutsunoBean.getSeq() != null){
					//情報更新
					cnt = genkinShutsunoDao.updateGenkinShutsuno(con, bSWSSession, genkinShutsunoBean);
				}else{
					if(BSWSUtils.isNotBlankNull(genkinShutsunoBean.getStrGenkinShutsunoKnj())
							&& BSWSUtils.isNotBlankNull(genkinShutsunoBean.getStrGenkinShutsunoHyojiJun())
							&& BSWSUtils.isNotBlankNull(genkinShutsunoBean.getStrGenkinShutsunoHyojiFlg())
					){
						//情報追加
						cnt = genkinShutsunoDao.insertGenkinShutsuno(con, null, bSWSSession, genkinShutsunoBean);
					}
				}
				if(cnt == 0){
					//処理なし
				}
			}

			//画面表示内容の設定
			hyojiJunbi(con);

			con.commit();
		}catch(SQLException se){
			con.rollback();
			throw se;
		}finally{
			con.close();
		}

		return SUCCESS;
	}

	public String deleteGenkinShutsuno() throws Exception{

		BSWSDao bSWSDao = BSWSDao.getInstance();

		//初期設定
		init();

		Connection con = DBUtils.getConnection();
		try{
			//現金出納削除
			int cnt = bSWSDao.deleteRow(con, intTaisyoSeq, " M_GENKIN_SHUTSUNO ");
			if(cnt == 0){
				//処理なし
			}

			//画面表示内容の設定
			hyojiJunbi(con);

			con.commit();
		}catch(SQLException se){
			con.rollback();
			throw se;
		}finally{
			con.close();
		}

		return SUCCESS;
	}

	private void hyojiJunbi(Connection con) throws Exception{

		//現金出納マスタ情報を取得
		getGenkinShutsunoJoho(con);

		//現金出納行追加
		GenkinShutsunoBean genkinShutsunoBean = new GenkinShutsunoBean();
		genkinShutsunoBean.setStrGroupCod(bSWSSession.getStrGroupCod());
		genkinShutsunoBean.setHyojiKbnList(BSWSConstants.hyojiKbnList);
		genkinShutsunoBeanList.add(genkinShutsunoBean);
	}

	private void getGenkinShutsunoJoho(Connection con) throws Exception{

		GenkinShutsunoDao genkinShutsunoDao = GenkinShutsunoDao.getInstance();
		this.genkinShutsunoBeanList = new ArrayList<GenkinShutsunoBean>();
		this.genkinShutsunoBeanList.clear();

		//現金出納マスタ情報を取得
		List<GenkinShutsunoModel> genkinShutsunoList = genkinShutsunoDao.getGenkinShutsunoJoho(con, null, bSWSSession.getStrGroupCod(), null);
		for(int i = 0; i < genkinShutsunoList.size(); i++){
			GenkinShutsunoBean genkinShutsunoBean = new GenkinShutsunoBean();
			genkinShutsunoBean.setSeq(genkinShutsunoList.get(i).getSeq());
			genkinShutsunoBean.setStrGroupCod(bSWSSession.getStrGroupCod());
			genkinShutsunoBean.setStrGenkinShutsunoKnj(genkinShutsunoList.get(i).getStrGenkinShutsunoKnj());
			genkinShutsunoBean.setStrGenkinShutsunoRyakuKnj(genkinShutsunoList.get(i).getStrGenkinShutsunoRyakuKnj());
			genkinShutsunoBean.setStrGenkinShutsunoHyojiJun(String.valueOf(genkinShutsunoList.get(i).getIntGenkinShutsunoHyojiJun()));
			genkinShutsunoBean.setStrGenkinShutsunoHyojiFlg(genkinShutsunoList.get(i).getStrGenkinShutsunoHyojiFlg());
			genkinShutsunoBean.setHyojiKbnList(BSWSConstants.hyojiKbnList);
			this.genkinShutsunoBeanList.add(genkinShutsunoBean);
		}
	}

	public BSWSSession getbSWSSession() {
		return bSWSSession;
	}

	public void setbSWSSession(BSWSSession bSWSSession) {
		this.bSWSSession = bSWSSession;
	}

	public List<GenkinShutsunoBean> getGenkinShutsunoBeanList() {
		return genkinShutsunoBeanList;
	}

	public void setGenkinShutsunoBeanList(
			List<GenkinShutsunoBean> genkinShutsunoBeanList) {
		this.genkinShutsunoBeanList = genkinShutsunoBeanList;
	}

	public Integer getIntTaisyoSeq() {
		return intTaisyoSeq;
	}

	public void setIntTaisyoSeq(Integer intTaisyoSeq) {
		this.intTaisyoSeq = intTaisyoSeq;
	}

}